package com.safe.ocs.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TimerTask;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.safe.ocs.beans.LaterMSG;
import com.safe.ocs.utils.HibernateUtil;

/**
 * 获取考勤人员所有信息表
 * 
 */
public class FindLaterMsg extends TimerTask {

	@Override
	public void run() {
		Session session = HibernateUtil.getSession();
		Transaction tr = session.beginTransaction();
		String findname = "select distinct t1.person_ID,t2.person_Name,t3.class_No ,t4.sect1_InTime,t4.sect1_OutTime, t1.brush_Date,t1.brush_Time from KQ_Download t1 left join ST_Person t2 on t1.person_ID = t2.person_No left join KQ_Person t3 on t1.person_ID=t3.Person_ID left join KQ_Class t4 on t4.Class_No=t3.Class_No where t1.Brush_Date=(select convert(varchar(10),getdate(),120))";
        //String findnames = "select distinct down.Card_No,st.Person_ID,st.Person_Name,kq.Class_No,class.sect1_InTime,class.sect1_OutTime from KQ_Download down left join ST_Person st on down.Card_No = st.Card_SN left join KQ_Person kq on st.Person_ID = kq.Person_ID left join KQ_Class class on class.Class_No=kq.Class_No where class.Class_No is not null";
        //修改后的sql语句，之前的Download里的person_ID不可用换用Card_No来匹配
		int person_Id = 0;
		String brush_date = null;
		try {
			List inputList = session.createSQLQuery(findname).list();

			if (inputList.size() > 0) {
				for (int i = 0; i < inputList.size(); i++) {
					LaterMSG laterMsg = new LaterMSG();
					Object[] msgArray = (Object[]) inputList.get(i);
					int person_id = (Integer) msgArray[0];
					String person_name = ((String) msgArray[1]).trim();
					String class_No = ((String) msgArray[2]).trim();
					String sect_InTime = ((String) msgArray[3]).trim();
					String sect_OutTime = ((String) msgArray[4]).trim();
					String brush_Date = ((String) msgArray[5]).trim();
					String brush_Time = ((String) msgArray[6]).trim();
					System.out.println("用户编号：" + person_id + "用户姓名："
							+ person_name + "班次编号：" + class_No + "规定上班时间："
							+ sect_InTime + "规定下班时间:" + sect_OutTime + "打卡日期："
							+ brush_Date + "打卡时间:" + brush_Time);
					
					String findLater = "from LaterMSG where person_ID=? and brush_Time=?";//判断数据库是否有该人员当天打卡的数据
					Query query = session.createQuery(findLater);
					query.setLong(0, person_id);
					query.setString(1, brush_Time);
					List laterList = query.list();
					if (laterList.size() == 0) {
						laterMsg.setPerson_ID(person_id);
						laterMsg.setPerson_Name(person_name);
						laterMsg.setSect_InTime(sect_InTime);
						laterMsg.setSect_OutTime(sect_OutTime);
						laterMsg.setBrush_Date(brush_Date);
						laterMsg.setBrush_Time(brush_Time);

						System.out.println("用户编号：" + laterMsg.getPerson_ID()
								+ "用户姓名:" + laterMsg.getPerson_Name() + "上班时间:"
								+ laterMsg.getSect_InTime() + "下班时间:"
								+ laterMsg.getSect_OutTime() + "打卡日期:"
								+ laterMsg.getBrush_Date() + "打卡时间:"
								+ laterMsg.getBrush_Time());
						session.save(laterMsg);
					}
				}
			}

			tr.commit();
			session.close();
		} catch (Exception e) {
			// tr.rollback();
			e.printStackTrace();
		}
	}

}
